package com.ruoyi.ui.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ui.domain.vo.AreaVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ui.domain.UiArea;
import com.ruoyi.ui.service.IUiAreaService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 地址Controller
 *
 * @author ruoyi
 * @date 2022-09-07
 */
@Api("地址管理")
@RestController
@RequestMapping("/area")
public class UiAreaController extends BaseController
{
    @Autowired
    private IUiAreaService uiAreaService;

    /**
     * 查询地址列表
     */
    @RequiresPermissions("ui:area:list")
    @ApiOperation("list")
    @GetMapping("/list/{level}")
//     public TableDataInfo list(UiArea uiArea)
//    {
//        startPage();
//        List<UiArea> list = uiAreaService.selectUiAreaList(uiArea);
//        return getDataTable(list);
//    }
    public TableDataInfo list(@PathVariable(name = "level") int level){
        List<UiArea> findlist = uiAreaService.findlist(level);
        List<AreaVo> reslist=new ArrayList<>();
        for (UiArea uiArea : findlist) {
            //er级列表
            List<AreaVo> reslist1=new ArrayList<>();
            AreaVo areaVo = new AreaVo();
            areaVo.setLabel(uiArea.getAreaName());
            System.out.println(uiArea.getAreaName());
            List<UiArea> second = uiAreaService.findChildByParentId(uiArea.getAreaId());
            for (UiArea area : second) {
                AreaVo areaVo1 = new AreaVo();
                areaVo1.setLabel(area.getAreaName());
                //san级列表
                List<AreaVo> reslist2=new ArrayList<>();
                List<UiArea> third = uiAreaService.findChildByParentId(area.getAreaId());
                for (UiArea uiArea1 : third) {
                    AreaVo areaVo2 = new AreaVo();
                    areaVo2.setLabel(uiArea1.getAreaName());
                    reslist2.add(areaVo2);
                }
                areaVo1.setChildren(reslist2);
                reslist1.add(areaVo1);

//                area.setAreas(third);
            }
            areaVo.setChildren(reslist1);
            reslist.add(areaVo);
//            uiArea.setAreas(second);
        }
        return getDataTable(reslist);
    }

    /**
     * 导出地址列表
     */
    @RequiresPermissions("ui:area:export")
    @Log(title = "地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UiArea uiArea)
    {
        List<UiArea> list = uiAreaService.selectUiAreaList(uiArea);
        ExcelUtil<UiArea> util = new ExcelUtil<UiArea>(UiArea.class);
        util.exportExcel(response, list, "地址数据");
    }

    /**
     * 获取地址详细信息
     */
    @RequiresPermissions("ui:area:query")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") Long areaId)
    {
        return AjaxResult.success(uiAreaService.selectUiAreaByAreaId(areaId));
    }


    /**
     * 新增地址
     */
    @RequiresPermissions("ui:area:add")
    @Log(title = "地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UiArea uiArea)
    {


        return toAjax(uiAreaService.insertUiArea(uiArea));
    }

    /**
     * 修改地址
     */
    @RequiresPermissions("ui:area:edit")
    @Log(title = "地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UiArea uiArea)
    {
        return toAjax(uiAreaService.updateUiArea(uiArea));
    }

    /**
     * 删除地址
     */
    @RequiresPermissions("ui:area:remove")
    @Log(title = "地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaIds}")
    public AjaxResult remove(@PathVariable Long[] areaIds)
    {
        return toAjax(uiAreaService.deleteUiAreaByAreaIds(areaIds));
    }


    @RequiresPermissions("ui:area:pid")
    @GetMapping("findbypid")
    public  TableDataInfo findbypid(@RequestParam(name = "parentId",required = false) Long parentId){

        List<UiArea> list = uiAreaService.findChildByParentId(parentId);
        return getDataTable(list);

    }
    @RequiresPermissions("ui:area:level")
    @GetMapping("findbylevel")
    public  TableDataInfo findbylevel(@RequestParam(name="level",required = false) Integer level){

        List<UiArea> list = uiAreaService.findlist(level);
        return getDataTable(list);

    }
}
